package com.project2.service;

import com.project2.bean.bo.CategoryAddBo;
import com.project2.bean.bo.CategoryUpdateBo;
import com.project2.bean.bo.ChildrenInCategoryUpdateBo;
import com.project2.bean.po.MarketCategory;
import com.project2.bean.vo.*;
import com.project2.mapper.MarketCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zhangminghui
 * @date 2022/8/13
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {
    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public DataInAdminCategoryVo queryAllCategoryInfo() {
        // todo: 查询出所有level为“L1”，并且deleted字段为false
        String firstLevel = "L1";
        Boolean deleted = false;
        List<ListInDataInAdminCategoryVo> listInDataInAdminCategoryVos = marketCategoryMapper.queryListInDataInAdminCategoryVoByLevelAndDeleted(firstLevel,deleted);
        DataInAdminCategoryVo dataInAdminCategoryVo = new DataInAdminCategoryVo();
        dataInAdminCategoryVo.setTotal(16);
        dataInAdminCategoryVo.setPages(1);
        dataInAdminCategoryVo.setLimit(16);
        dataInAdminCategoryVo.setPage(1);
        dataInAdminCategoryVo.setList(listInDataInAdminCategoryVos);

        String secondLevel = "L2";
        for (int i = 0; i < dataInAdminCategoryVo.getList().size(); i++) {
            List<ChildrenInListInDataInAdminCategoryVo> childrenInListInDataInAdminCategoryVos = marketCategoryMapper.queryChildrenInListInDataInAdminCategoryVoByPidAndLevelAndDeleted(
                    dataInAdminCategoryVo.getList().get(i).getId(), secondLevel,deleted);
            dataInAdminCategoryVo.getList().get(i).setChildren(childrenInListInDataInAdminCategoryVos);
        }
        return dataInAdminCategoryVo;
    }

    @Override
    public void addCategoryByBo(CategoryAddBo categoryAddBo) {
        Date date = new Date();
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setName(categoryAddBo.getName());
        marketCategory.setKeywords(categoryAddBo.getKeywords());
        marketCategory.setLevel(categoryAddBo.getLevel());
        marketCategory.setPid(categoryAddBo.getPid());
        marketCategory.setDesc(categoryAddBo.getDesc());
        marketCategory.setIconUrl(categoryAddBo.getIconUrl());
        marketCategory.setPicUrl(categoryAddBo.getPicUrl());
        marketCategory.setAddTime(date);
        marketCategory.setUpdateTime(date);
        //marketCategoryMapper.insert(marketCategory);
        // todo: 使用insertSelective，才会给空的字段设置默认值
        marketCategoryMapper.insertSelective(marketCategory);
    }

    @Override
    public DataInCategoryAddVo queryNewAddCategoryInfoByNameAndDesc(String name, String desc) {
        DataInCategoryAddVo dataInCategoryAddVo = marketCategoryMapper.queryCategoryByNameAndDesc(name, desc);
        return dataInCategoryAddVo;
    }

    @Override
    public void updateParentCategoryInfoByBo(CategoryUpdateBo categoryUpdateBo) {
        Date date = new Date();
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(categoryUpdateBo.getId());
        marketCategory.setName(categoryUpdateBo.getName());
        marketCategory.setKeywords(categoryUpdateBo.getKeywords());
        marketCategory.setDesc(categoryUpdateBo.getDesc());
        marketCategory.setIconUrl(categoryUpdateBo.getIconUrl());
        marketCategory.setPicUrl(categoryUpdateBo.getPicUrl());
        marketCategory.setLevel(categoryUpdateBo.getLevel());
        marketCategory.setUpdateTime(date);
        // todo: 不能setCategoryUpdateBo里的children
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
    }

    @Override
    public void updateChildrenCategoryInfo(ChildrenInCategoryUpdateBo beanInChildren) {
        Date date = new Date();
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(beanInChildren.getId());
        marketCategory.setName(beanInChildren.getName());
        marketCategory.setKeywords(beanInChildren.getKeywords());
        marketCategory.setDesc(beanInChildren.getDesc());
        marketCategory.setIconUrl(beanInChildren.getIconUrl());
        marketCategory.setPicUrl(beanInChildren.getPicUrl());
        marketCategory.setLevel(beanInChildren.getLevel());
        marketCategory.setUpdateTime(date);
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
    }

    @Override
    public void updateChildCategoryInfoByBo(CategoryUpdateBo categoryUpdateBo) {
        Date date = new Date();
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(categoryUpdateBo.getId());
        marketCategory.setName(categoryUpdateBo.getName());
        marketCategory.setKeywords(categoryUpdateBo.getKeywords());
        marketCategory.setDesc(categoryUpdateBo.getDesc());
        marketCategory.setIconUrl(categoryUpdateBo.getIconUrl());
        marketCategory.setPicUrl(categoryUpdateBo.getPicUrl());
        marketCategory.setLevel(categoryUpdateBo.getLevel());
        marketCategory.setPid(categoryUpdateBo.getPid());
        marketCategory.setUpdateTime(date);
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
    }

    @Override
    public DataInCategoryL1Vo queryAllL1LevelCategoryInfo() {
        DataInCategoryL1Vo dataInCategoryL1Vo = new DataInCategoryL1Vo();
        dataInCategoryL1Vo.setTotal(14);
        dataInCategoryL1Vo.setPages(1);
        dataInCategoryL1Vo.setLimit(14);
        dataInCategoryL1Vo.setPage(1);
        String firstLevel = "L1";
        List<ListInDataInCategoryL1Vo> listInDataInCategoryL1Vos = marketCategoryMapper.queryAllL1LevelCategoryInfoByLevel(firstLevel);
        dataInCategoryL1Vo.setList(listInDataInCategoryL1Vos);
        return dataInCategoryL1Vo;
    }

    @Override
    public void deleteParentCategory(Integer id) {
        // todo: 逻辑删除
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(id);
        marketCategory.setDeleted(true);
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
    }

    @Override
    public void deleteChildCategory(Integer id) {
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(id);
        marketCategory.setDeleted(true);
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
    }

}
